package io.esirong.widget.media;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import io.esirong.widget.R;


/**
 * 录制视频的按钮,长按录制,点击拍照
 */
public class CaptureButton4 extends FrameLayout {
    public final String TAG = "CaptureButton";
    private CaptureListener mCaptureListener;

    public CaptureButton4(Context context) {
        super(context);
        init();
    }

    public CaptureButton4(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CaptureButton4(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CaptureButton4(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_media_camera_view, null, false);
        CaptureButton captureButton3 = view.findViewById(R.id.capture_btn);
        captureButton3.setRecodeDuration(6000);
        captureButton3.setCaptureListener(new CaptureButton.CaptureListener() {
            @Override
            public void capture() {

            }

            @Override
            public void cancel() {
                onBtnCancel();
            }

            @Override
            public void determine() {
                onBtnConfirm();
            }

            @Override
            public void quit() {

            }

            @Override
            public void record() {
                onBtnClick();
            }

            @Override
            public void recordEnd() {
                onBtnClick();
            }

            @Override
            public void getRecordResult() {
                onBtnConfirm();
            }

            @Override
            public void deleteRecordResult() {

            }

            @Override
            public void scale(float scaleValue) {

            }
        });
        addView(view);
        initButton();
    }

    public void initButton() {
        findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBtnClick();
            }
        });
        findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBtnCancel();
            }
        });
        findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCaptureListener != null) {
                    mCaptureListener.cancel();

                }
            }
        });
        findViewById(R.id.btn4).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCaptureListener != null) {
                    mCaptureListener.capture();

                }
            }
        });
        invalidate();
    }

    private void onBtnClick() {
        if (!isEnabled()) {
            return;
        }
        if (mCaptureListener != null) {
            mCaptureListener.record();
        }
    }

    public void start() {

    }

    private void onBtnCancel() {
        Log.w(TAG, "点击重新录制");
        if (mCaptureListener != null) {
            mCaptureListener.deleteRecordResult();
        }
    }

    private void onBtnConfirm() {
        Log.w(TAG, "点击确认录制");
        if (mCaptureListener != null) {
            mCaptureListener.getRecordResult();
        }
    }

    private class LongPressRunnable implements Runnable {
        @Override
        public void run() {
        }
    }


    public void setCaptureListener(CaptureButton4.CaptureListener mCaptureListener) {
        this.mCaptureListener = mCaptureListener;
    }

    public interface CaptureListener {
        void capture();

        void cancel();//拍照点击返回

        void determine();//拍照的确定

        void quit();//关闭界面

        void record();

        void recordEnd();

        void getRecordResult();//录像点击确定

        void deleteRecordResult();//录像点击返回

        void scale(float scaleValue);
    }
}